package service;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import common.kit.PageSql;
import model.Calibration;
import model.Staff;
import model.Station;

public class CalibrationService extends BaseService {

	public Page<Record> getPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tc.id,tc.createTime,tc.principle,tc.range,tc.unit,tc.zero,tc.lastNum,tc.beforeNum,tc.afterNum,tc.driven,tc.isNormal,tc.gas,tc.rangLastNum,tc.rangebeforeNum,tc.rangeAfterNum,tc.rangeNum,tc.rangeIsNormal,ts.name,tcy.name AS companyName1,tsr.name AS servicerName,tc.recorder " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_calibration tc ") ;
	    pageSql.join(" t_station ts " , " ts.id = tc.stationId ") ;
	    pageSql.join(" t_servicer tsr " , " tsr.id = tc.servicerId ") ;
	    pageSql.join(" t_company tcy " , " tcy.id = ts.companyId ") ;
		pageSql.setNotLessThanCondition("tc.createTime","tc.createTime.start") ; //创建时间
		pageSql.setNotMoreThanCondition("tc.createTime","tc.createTime.end") ; //创建时间
		pageSql.setEqualCondition("tc.stationId") ; //站点	 
		pageSql.setEqualCondition("tc.servicerId") ; //运维公司	 
		pageSql.setLikeCondition("tsr.name") ; //运维公司	 
		pageSql.setLikeCondition("tcy.name") ; //企业名称	 
	    pageSql.orderBy(" tc.createTime DESC ");
		return pageSql.paginate( pageNumber , pageSize) ;
	}

	public Page<Record> getApiPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tc.id,tc.createTime,tc.recorder,tc.principle,tc.range,tc.unit,tc.zero,tc.lastNum,tc.beforeNum,tc.afterNum,tc.driven,tc.isNormal,tc.gas,tc.rangLastNum,tc.rangebeforeNum,tc.rangeAfterNum,tc.rangeNum,tc.rangeIsNormal,ts.name " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_calibration tc ") ;
	    pageSql.join(" t_servicer ts " , " ts.id = tc.servicerId ") ;
		pageSql.setEqualCondition("tc.stationId") ; //站点	 
		pageSql.setNotLessThanCondition("tc.createTime", "startTime");
		pageSql.setNotMoreThanCondition("tc.createTime", "endTime");
	    pageSql.orderBy(" tc.createTime DESC ");
		return pageSql.paginate( pageNumber , pageSize) ;
	}

	public Ret save(Calibration calibration , Staff staff , Station station) {
		calibration.setServicerId(staff.getServicerId());
		calibration.setRecorder(staff.getName());
		calibration.save();
		//保存维护记录
		new MaintainRecordsService().saveGasQuatity(calibration, station, staff);
		return ok() ;
	}
}
